/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.prerecitran.service;

import java.util.List;

import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.prerecitran.entity.TadvancesReceivedTransfer;
import com.jeesite.modules.prerecitran.dao.TadvancesReceivedTransferDao;
import com.jeesite.modules.prerecitran.entity.TadvancesReceivedTransferDetail;
import com.jeesite.modules.prerecitran.dao.TadvancesReceivedTransferDetailDao;

/**
 * 预收转账单Service
 * @author wsm
 * @version 2021-03-24
 */
@Service
@Transactional(readOnly=true)
public class TadvancesReceivedTransferService extends CrudService<TadvancesReceivedTransferDao, TadvancesReceivedTransfer> {
	
	@Autowired
	private TadvancesReceivedTransferDetailDao tadvancesReceivedTransferDetailDao;
	@Autowired
	private CodeRuleService codeRuleService;
	/**
	 * 获取单条数据
	 * @param tadvancesReceivedTransfer
	 * @return
	 */
	@Override
	public TadvancesReceivedTransfer get(TadvancesReceivedTransfer tadvancesReceivedTransfer) {
		TadvancesReceivedTransfer entity = super.get(tadvancesReceivedTransfer);
		if (entity != null){
			TadvancesReceivedTransferDetail tadvancesReceivedTransferDetail = new TadvancesReceivedTransferDetail(entity);
			tadvancesReceivedTransferDetail.setStatus(TadvancesReceivedTransferDetail.STATUS_NORMAL);
			entity.setTadvancesReceivedTransferDetailList(tadvancesReceivedTransferDetailDao.findList(tadvancesReceivedTransferDetail));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param tadvancesReceivedTransfer 查询条件

	 * @return
	 */
	@Override
	public Page<TadvancesReceivedTransfer> findPage(TadvancesReceivedTransfer tadvancesReceivedTransfer) {
		return super.findPage(tadvancesReceivedTransfer);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param tadvancesReceivedTransfer
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(TadvancesReceivedTransfer tadvancesReceivedTransfer) {

		if(tadvancesReceivedTransfer.getIsNewRecord()){
			tadvancesReceivedTransfer.setAdvancesReceivedTransferCode(codeRuleService.getCode("releasecar003"));
			tadvancesReceivedTransfer.setCreateBy(UserUtils.getUser().getLoginCode());
			//设置成编辑状态
			tadvancesReceivedTransfer.setBillStatus("1");
		}
		super.save(tadvancesReceivedTransfer);
		if (tadvancesReceivedTransfer.getIsNewRecord()){
			codeRuleService.updateCurrentNumber("releasecar003");
		}

		// 保存 TadvancesReceivedTransfer子表
		for (TadvancesReceivedTransferDetail tadvancesReceivedTransferDetail : tadvancesReceivedTransfer.getTadvancesReceivedTransferDetailList()){
			if (!TadvancesReceivedTransferDetail.STATUS_DELETE.equals(tadvancesReceivedTransferDetail.getStatus())){
				tadvancesReceivedTransferDetail.setAdvancesReceivedTransferId(tadvancesReceivedTransfer);
				if (tadvancesReceivedTransferDetail.getIsNewRecord()){
					tadvancesReceivedTransferDetailDao.insert(tadvancesReceivedTransferDetail);
				}else{
					tadvancesReceivedTransferDetailDao.update(tadvancesReceivedTransferDetail);
				}
			}else{
				tadvancesReceivedTransferDetailDao.delete(tadvancesReceivedTransferDetail);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param tadvancesReceivedTransfer
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(TadvancesReceivedTransfer tadvancesReceivedTransfer) {
		super.updateStatus(tadvancesReceivedTransfer);
	}
	
	/**
	 * 删除数据
	 * @param tadvancesReceivedTransfer
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(TadvancesReceivedTransfer tadvancesReceivedTransfer) {
		super.delete(tadvancesReceivedTransfer);
		TadvancesReceivedTransferDetail tadvancesReceivedTransferDetail = new TadvancesReceivedTransferDetail();
		tadvancesReceivedTransferDetail.setAdvancesReceivedTransferId(tadvancesReceivedTransfer);
		tadvancesReceivedTransferDetailDao.deleteByEntity(tadvancesReceivedTransferDetail);
	}
	
}